﻿@using FluentValidation

<MudForm Model="@_model" Validation="@(_validationRules.ValidateValue)">
    <MudTextField @bind-Value="_model.String1" For="(() => _model.String1)" />
    <MudTextField @bind-Value="_model.String2" />
    <MudDatePicker @bind-Date="_model.DateTime1" For="(() => _model.DateTime1)" />
    <MudDatePicker @bind-Date="_model.DateTime2" />
</MudForm>

@code {
    private readonly TestClass _model = new();
    private readonly TestClassValidator _validationRules = new();

    public class TestClassValidator : AbstractValidator<TestClass>
    {
        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<TestClass>.CreateWithOptions((TestClass)model, x => x.IncludeProperties(propertyName)));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }

    public class TestClass
    {
        public string? String1 { get; set; }

        public string? String2 { get; set; }

        public DateTime? DateTime1 { get; set; }

        public DateTime? DateTime2 { get; set; }
    }
}